package scripts;

import api.EmpApi;
import api.LoginApi;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.Test;
import util.HttpClientUtil;
import util.ResponseData;

import java.io.IOException;
import java.net.URISyntaxException;

public class TestEmp {
    private  final LoginApi loginApi=new LoginApi();
    private  final EmpApi empapi=new EmpApi();
    private String emp_id=null;
    private static Logger logger= LoggerFactory.getLogger(TestEmp.class);
    @Test
    public static void test_01() throws IOException, URISyntaxException {
        ResponseData responseData = LoginApi.login("13800000002","123456");
        logger.debug("responseData={}",responseData);
        String token= responseData.getBody().getString("data");
        HttpClientUtil.build_header("Authorization","Bearer "+token);
    }
    @Test
    public void test_02() throws IOException, URISyntaxException {
        String iphone= RandomStringUtils.random(8);
        ResponseData responseData= EmpApi.addEmp("HAO","181"+iphone,"TEST0001");
        logger.debug("responseData={}",responseData);
        Assert.assertTrue(responseData.getBody().getBoolean("success"));
        Assert.assertEquals(responseData.getBody().getIntValue("code"),10000);
        Assert.assertTrue(responseData.getBody().getString("message").contains("成功"));
        emp_id=responseData.getBody().getJSONObject("data").getString("id");

    }
    @Test
    public void test_03() throws IOException, URISyntaxException {
        ResponseData responseData=EmpApi.queryEmp(emp_id);
        logger.debug("responseData={}",responseData);
        Assert.assertTrue(responseData.getBody().getBoolean("success"));
        Assert.assertEquals(responseData.getBody().getIntValue("code"),10000);
        Assert.assertTrue(responseData.getBody().getString("message").contains("成功"));
    }
    @Test
    public void test_04() throws IOException, URISyntaxException {
        ResponseData responseData=EmpApi.putEmp("test111",emp_id);
        logger.debug("responseData={}",responseData);
        Assert.assertTrue(responseData.getBody().getBoolean("success"));
        Assert.assertEquals(responseData.getBody().getIntValue("code"),10000);
        Assert.assertTrue(responseData.getBody().getString("message").contains("成功"));
    }
    @Test
    public void test_05() throws IOException, URISyntaxException {
        ResponseData responseData=EmpApi.delete(emp_id);
        logger.debug("responseData={}",responseData);
        Assert.assertTrue(responseData.getBody().getBoolean("success"));
        Assert.assertEquals(responseData.getBody().getIntValue("code"),10000);
        Assert.assertTrue(responseData.getBody().getString("message").contains("成功"));
    }
}
